home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr52 / wildcard.zip / CLIP5VER.PRG next >
Text File  |  1993-04-02  |  3KB  |  69 lines

  1. /* Function:  [<logical exp>] = K_WILDCARD(<char exp1>,<char exp2>)
  2.    Params  :  <char exp1> is the target string to search
  3.               <char exp2> is the mask to search the target with
  4.    Purpose :  Provide an alternative to concatenated SUBSTR() comparisons
  5.               for the purpose of filtering databases primarily for SET
  6.               FILTER TO or FOR/WHILE conditions.  Basically anywhere
  7.               string comparisons are needed.
  8.    Example :  REPORT FORM test TO PRINT FOR k_wildcard(textfield,"?89?i?")
  9.    Returns :  a logical .T. or .F.
  10.    Author  :  Kim Bjork, AMG Inc., 12/23/90, CIS 74017,1576
  11.            :  Released for public usage.  No warranty implied or granted.
  12.    Notes   : -<char exp1> can be any valid char string, ie. memvar or field.
  13.              -<char exp2>, the mask, must be formed with question marks,
  14.               ie. "??IS????", "32??????", "?whEre?iS??", etc.
  15.               The example, "?whEre?iS??", is where this function makes
  16.               a difference, otherwise, use SUBSTR().
  17.               Of course, it will work without any wildcard characters
  18.               but then why use it?
  19.              -Any printable character other than a question mark
  20.               is considered valid search criteria (a significant char).
  21.              -If the mask string is longer than the target string, the
  22.               mask string will be truncated to the length of the target.
  23.               Processing will be slowed slightly if the mask string is
  24.               longer than the target (more code executes).
  25.              -The target and mask strings will be forced to upper case
  26.               for the purpose of the search.
  27.              -To speed up processing, remove the TYPE() checking portion
  28.               of the code.  If you want to take responsibility at the 
  29.               calling portion of your code, remove the mask length checking
  30.               also.
  31.              -This function in entirely inappropriate for large character
  32.               strings.  Was designed for memvar lengths of 15 chars or
  33.               less.  The longer the mask, the more iterations the 
  34.               FOR..NEXT loop has to perform.
  35.              -CLIPPER 5 version. */
  36.  
  37. function k_wildcard( t_, m_ )
  38.    local tl, ml, i
  39.  
  40.    // check parameters
  41.    if valtype("t_") + valtype("m_") != "CC"
  42.       retu .F.
  43.    endif
  44.  
  45.    tl := len(t_)                   // target length
  46.    ml := len(m_)                   // mask length
  47.  
  48.    if ml > tl                      // mask longer than target?
  49.       ml := tl                     // mask length becomes same as target
  50.       m_ := substr(m_,1,tl)        // truncate to target length
  51.    endif
  52.  
  53.    // force to upper case
  54.    t_ := upper(t_)
  55.    m_ := upper(m_)
  56.  
  57.    // replace the target with "?" chars from the mask (where appropriate)
  58.    for i := 1 to ml
  59.       if substr(m_,i,1) = "?"      // not a significant char?
  60.          t_ := stuff(t_,i,1,"?")   // replace target char with "?"
  61.       endif
  62.    next
  63.  
  64.    // substring of target string same as the mask?
  65. return iif(substr(t_,1,ml) = m_, .T., .F.)
  66.  
  67. // I would be very interested in improvements to this function.
  68.  
  69.